package com.ruoyi.web.controller.weiriya;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.weiriya.domain.WArea;
import com.ruoyi.weiriya.service.IWAreaService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 小区信息（停车点位）Controller
 * 
 * @author lynn
 * @date 2023-08-01
 */
@Api(tags = "小区信息（停车点位）管理")
@RestController
@RequestMapping("/weiriya/area")
public class WAreaController extends BaseController
{
    @Autowired
    private IWAreaService wAreaService;

    /**
     * 查询小区信息（停车点位）列表
     */
    @ApiOperation("获取管理员所辖小区信息（停车点位）列表")
    @PreAuthorize("@ss.hasPermi('weiriya:area:list')")
    @GetMapping("/list")
    public TableDataInfo list(WArea wArea)
    {
        startPage();
        List<WArea> list = wAreaService.getList(wArea);
        return getDataTable(list);
    }

    /**
     * 导出小区信息（停车点位）列表
     */
    @ApiOperation("导出小区信息（停车点位）列表")
    @PreAuthorize("@ss.hasPermi('weiriya:area:export')")
    @Log(title = "小区信息（停车点位）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WArea wArea)
    {
        List<WArea> list = wAreaService.getList(wArea);
        ExcelUtil<WArea> util = new ExcelUtil<WArea>(WArea.class);
        util.exportExcel(response, list, "小区信息（停车点位）数据");
    }

    /**
     * 导入小区信息（停车点位）列表
     */
    @ApiOperation("导入小区信息（停车点位）列表")
    @PreAuthorize("@ss.hasPermi('weiriya:area:import')")
    @Log(title = "小区信息（停车点位）", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<WArea> util = new ExcelUtil<WArea>(WArea.class);
        List<WArea> list = util.importExcel(file.getInputStream());
        return success(wAreaService.saveBatch(list));
    }

    /**
     * 获取小区信息（停车点位）详细信息
     */
    @ApiOperation("获取小区信息（停车点位）详细")
    @ApiImplicitParam(name = "id", value = "小区信息（停车点位）ID", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('weiriya:area:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(wAreaService.getWAreaById(id));
    }

    /**
     * 新增小区信息（停车点位）
     */
    @ApiOperation("新增小区信息（停车点位）")
    @PreAuthorize("@ss.hasPermi('weiriya:area:add')")
    @Log(title = "小区信息（停车点位）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WArea wArea)
    {
        if(StringUtils.isBlank(wArea.getCompanyId())){
            throw new ServiceException("请选择公司！");
        }
        return toAjax(wAreaService.saveWArea(wArea));
    }

    /**
     * 修改小区信息（停车点位）
     */
    @ApiOperation("更新小区信息（停车点位）")
    @PreAuthorize("@ss.hasPermi('weiriya:area:edit')")
    @Log(title = "小区信息（停车点位）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WArea wArea)
    {
        wArea.setCompanyId(null);
        return toAjax(wAreaService.updateWAreaById(wArea));
    }

    /**
     * 删除小区信息（停车点位）
     */
    @ApiOperation("删除小区信息（停车点位）")
    @ApiImplicitParam(name = "ids", value = "小区信息（停车点位）ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('weiriya:area:remove')")
    @Log(title = "小区信息（停车点位）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(wAreaService.removeWAreaByIds(ids));
    }
}
